package com.example.pushmessage.controller;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@RestController
public class Producer {

    //由于ConfigurableBeanFactory.SCOPE_PROTOTYPE配置 无法自动注入 需要构造方法注入
    private RabbitTemplate rabbitTemplate;


    @Autowired
    public Producer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }


    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage(String message,Integer userId) {
        String messageId = String.valueOf(UUID.randomUUID());
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("createTime",createTime);
        map.put("message",message);
        map.put("userId",userId);
        //将消息携带绑定键值：wisdom_routingkey_message  发送到交换机wisdom_exchange_message
        rabbitTemplate.convertAndSend("wisdom_exchange_message", "wisdom_routingkey_message", map);
        return "ok";
    }
}
